﻿package view.tips
{
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;
    import flash.utils.*;

    public class TextMovie extends Sprite
    {
        private var t:Timer;
        public var txtinfo:TextField;
        private var str:String;
        private var counttotal:uint;
        private var tft:TextFormat;
        private var _clearFunc:Function = null;
        private var _timeUint:uint = 0;
        private var _filter:GlowFilter;
        private var _curW:int = 0;
        private var count:int = 1;
        private var position:int = 0;
        private static var _font:String = "null";
        private static var _fonts:Array = ["楷体", "宋体"];

        public function TextMovie(param1:String, param2:Function = null)
        {
            var _loc_3:Array = null;
            var _loc_4:String = null;
            this.txtinfo = new TextField();
            this._filter = new GlowFilter(0);
            this.tft = new TextFormat();
            this.tft.color = 16711680;
            this.tft.size = 22;
            this.tft.bold = true;
            if (_font == "null")
            {
                _loc_3 = Font.enumerateFonts(true);
                for each (_loc_4 in _fonts)
                {
                    
                    if (this.checkFontFamily(_loc_4, _loc_3))
                    {
                        _font = _loc_4;
                        break;
                    }
                }
            }
            if (_font != "null" && _font != "")
            {
                this.tft.font = _font;
            }
            this.txtinfo.defaultTextFormat = this.tft;
            this.txtinfo.autoSize = TextFieldAutoSize.RIGHT;
            this.txtinfo.htmlText = param1;
            this.str = this.txtinfo.text;
            this._curW = 0;
            this.counttotal = this.str.length;
            if (this.counttotal == 0)
            {
                return;
            }
            this.x = (-this.txtinfo.width) / 2;
            this.init();
            this._clearFunc = param2;
            return;
        }// end function

        private function checkFontFamily(param1:String, param2:Array) : Boolean
        {
            var _loc_3:Font = null;
            var _loc_4:String = null;
            var _loc_5:Boolean = false;
            var _loc_6:int = 0;
            var _loc_7:* = param2.length;
            while (_loc_6 < _loc_7)
            {
                
                _loc_3 = param2[_loc_6] as Font;
                _loc_4 = _loc_3.fontName;
                if (_loc_4 == param1)
                {
                    _loc_5 = true;
                    return _loc_5;
                }
                _loc_6++;
            }
            return _loc_5;
        }// end function

        public function init() : void
        {
            var _loc_1:* = 300 / this.counttotal;
            this.t = new Timer(_loc_1, this.counttotal);
            this.t.addEventListener(TimerEvent.TIMER, this.textfly);
            this.t.addEventListener(TimerEvent.TIMER_COMPLETE, this.timerEnd);
            this.t.start();
            return;
        }// end function

        public function textfly(event:TimerEvent) : void
        {
            var _loc_2:* = new TextField();
            _loc_2.text = this.str.charAt(0);
            _loc_2.autoSize = TextFieldAutoSize.LEFT;
            _loc_2.setTextFormat(this.txtinfo.getTextFormat(this.position, (this.position + 1)));
            _loc_2.selectable = false;
            _loc_2.filters = [this._filter];
            _loc_2.x = this._curW;
            var _loc_3:String = this;
            var _loc_4:* = this.count + 1;
            _loc_3.count = _loc_4;
            this._curW = this._curW + _loc_2.width;
            this.str = this.str.substr(1);
            addChild(_loc_2);
            TweenLite.from(_loc_2, 2, {y:"-20", alpha:0, ease:Elastic.easeOut});
            var _loc_3:String = this;
            var _loc_4:* = this.position + 1;
            _loc_3.position = _loc_4;
            return;
        }// end function

        public function timerEnd(event:TimerEvent) : void
        {
            this.t.removeEventListener(TimerEvent.TIMER, this.textfly);
            this.t.removeEventListener(TimerEvent.TIMER_COMPLETE, this.timerEnd);
            this.t.stop();
            this._timeUint = setTimeout(this.hide, 2000);
            return;
        }// end function

        public function clearTime() : void
        {
            if (this._timeUint > 0)
            {
                clearTimeout(this._timeUint);
            }
            return;
        }// end function

        private function hide() : void
        {
            TweenLite.to(this, 0.5, {alpha:0});
            this._timeUint = setTimeout(this.clear, 500);
            return;
        }// end function

        public function clear() : void
        {
            if (this._clearFunc != null)
            {
                while (this.numChildren > 0)
                {
                    
                    this.removeChildAt(0);
                }
                if (this.t)
                {
                    this.t.removeEventListener(TimerEvent.TIMER, this.textfly);
                    this.t.removeEventListener(TimerEvent.TIMER_COMPLETE, this.timerEnd);
                    this.t.stop();
                }
                this._clearFunc(this);
                this._clearFunc = null;
                this.txtinfo = null;
                if (this._timeUint > 0)
                {
                    clearTimeout(this._timeUint);
                }
            }
            return;
        }// end function

    }
}
